/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.metastore;

import cn.ac.iie.di.ban.commons.RuntimeEnv;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author ulss
 */
public class MetaStoreManager {

    private String db_driver;
    private String db_url;
    private String db_user;
    private String db_pwd;
    private Integer dbsource_initSize;
    private Integer dbsource_maxSize;
    private String ip;
    private Integer port;
    private Integer parallel;
    private static Logger logger = Logger.getLogger(MetaStoreManager.class.getName());
    private static MetaStoreManager metaStoreManager = null;

    public MetaStoreManager() {
    }

    public static MetaStoreManager getMetaStoreManger() {
        if (metaStoreManager == null) {
            try {
                metaStoreManager = new MetaStoreManager();
            } catch (Exception ex) {
                metaStoreManager = null;
                logger.error("initializing metaStoreManager is failed for " + ex, ex);
            }
        }
        return metaStoreManager;
    }

    public void init() {
        String mysql_common = (String) RuntimeEnv.getParam(RuntimeEnv.MYSQL_COMMON);
        String[] mysqls = mysql_common.split("[|]");
        if (mysqls.length != 6) {
            logger.error("mysql_common conf is error! need like this: db_driver|db_url|db_user|db_pwd|dbsource_initSize|dbsource_maxSize");
            System.exit(0);
        }
        db_driver = mysqls[0];
        db_url = mysqls[1];
        db_user = mysqls[2];
        db_pwd = mysqls[3];
        dbsource_initSize = Integer.parseInt(mysqls[4]);
        dbsource_maxSize = Integer.parseInt(mysqls[5]);
        String server_conf = (String) RuntimeEnv.getParam(RuntimeEnv.SERVER_CONF);
        String[] server = server_conf.split("[|]");
        ip = server[0];
        port = new Integer(server[1]);
        parallel = new Integer(server[2]);
    }

    public Map<String, Load2DBEntity> getLoad2DBRules() throws Exception {
        Map<String, Map<String, String>> columnsMappers = columnsMapperParse((String) RuntimeEnv.getParam(RuntimeEnv.GET_DB_COLUMNS));
//        List<Map<String, Load2DBEntity>> confGetTables = new ArrayList();
        Map<String, Load2DBEntity> confGetTables = new HashMap<>();
        String mysql_conf = (String) RuntimeEnv.getParam(RuntimeEnv.MYSQL_CONF);
        String[] mysqlConf = mysql_conf.split("[#]");
        for (String confOne : mysqlConf) {//conf=se2record;test.record|mysql|1|20
            String[] confStrings = confOne.split("[;]");
            String entityName = confStrings[0];
            String[] entityCols = confStrings[1].split("[|]");
            if (entityCols.length == 4) {
                logger.info("new mysql entity : " + entityName);
                confGetTables.put(entityName, new Load2DBEntity(entityName, entityCols[0], entityCols[2], entityCols[3], entityCols[1], columnsMappers.get(entityName)));

            } else {
                logger.error("please check conf_mysql !!! ");
                System.exit(0);
            }
        }
        return confGetTables;
    }

    /*
    字段映射解析
    get_db_columns=se2record;req_id,req_id:type,type:sql,sql:Status,Status:Detail,Detail:result,result:task_id,task_id:timestamp,timestamp:uniq_seq,uniq_seq
     */
    public Map<String, Map<String, String>> columnsMapperParse(String columnsString) {
        Map<String, Map<String, String>> columnMaps = new HashMap<>();
        if (columnsString == null || "".equals(columnsString)) {
            return columnMaps;
        }
        Map<String, String> columnMap = new HashMap<>();
        String[] columnsStrings = columnsString.split("[#]");
        for (String one : columnsStrings) {
            String[] columnsStringsOne = one.split("[;]");
            String entityName = columnsStringsOne[0];
            String[] columns = columnsStringsOne[1].split("[:]");
            for (String column : columns) {
                String[] colOne = column.split("[,]");
                columnMap.put(colOne[0], colOne[1]);
            }
            columnMaps.put(entityName, columnMap);
        }
        return columnMaps;
    }

    public String getDb_driver() {
        return db_driver;
    }

    public String getDb_url() {
        return db_url;
    }

    public String getDb_user() {
        return db_user;
    }

    public String getDb_pwd() {
        return db_pwd;
    }

    public Integer getDbsource_initSize() {
        return dbsource_initSize;
    }

    public Integer getDbsource_maxSize() {
        return dbsource_maxSize;
    }

    public Integer getPort() {
        return port;
    }

    public Integer getParallel() {
        return parallel;
    }

     public String getIp() {
        return ip;
    }

}
